package com.atu.poker.dao.impl;

import java.util.List;

import com.atu.poker.dao.DAO;
import com.atu.poker.dao.RoomSeatDAO;
import com.atu.poker.po.Seat;

public class RoomSeatDAOJdbcImpl extends DAO<Seat> implements RoomSeatDAO {

	@Override
	public void insertOwnerUser(String seatId,String roomId, String userId, String seatNo){
		String sql = "INSERT INTO t_seat(seat_id,room_id,user_id,seat_no) VALUES(?,?,?,?)";
		update(sql,seatId, roomId, userId,seatNo);
	}

	@Override
	public void insertOtherUser(String roomId, String userId, String seatNo){
		String sql = "";
		update(sql, roomId, userId,seatNo);
	}

	@Override
	public long userCount(String roomId){
		String sql = "SELECT COUNT(room_id) userCount FROM t_seat WHERE room_id=?";
		return getForValue(sql, roomId);
	}

	@Override
	public Seat getSelfSeat(String seatId) {
		String sql = "SELECT s.seat_id seatId,s.room_id roomId,r.user_limit userLimit,r.upper_chip upperChip,r.lower_chip lowerChip,(SELECT user_name FROM t_user WHERE user_id = s.user_id) userName, s.seat_no seatNo FROM t_seat s LEFT JOIN t_room r ON r.room_id = s.room_id WHERE s.seat_id = ?";
		return get(sql, seatId);
	}

	@Override
	public List<Seat> getOtherSeatMap(String roomId,String seatNo) {
		String sql = "SELECT s.room_id roomId,(SELECT user_name FROM t_user WHERE user_id=s.user_id) userName, s.seat_no seatNo FROM t_seat s WHERE s.room_id=? AND s.seat_no!=? ORDER BY s.seat_no ASC";
		return getForList(sql, roomId,seatNo);
	}

	@Override
	public String getSeatId(String roomId, String seatNo) {
		String sql = "SELECT seat_id seatId FROM t_seat WHERE room_id=? AND seat_no=?";
		return getForValue(sql, roomId,seatNo);
	}

	@Override
	public String getSeatNo(String seatId) {
		String sql = "SELECT seat_no seatNo FROM t_seat WHERE seat_id=?";
		return getForValue(sql, seatId);
	}

	@Override
	public String getRoomId(String seatId) {
		String sql = "SELECT room_id roomId FROM t_seat WHERE seat_id=?";
		return getForValue(sql, seatId);
	}

	@Override
	public String getUserLimit(String seatId) {
		String sql = "SELECT (SELECT r.user_limit FROM t_room r WHERE r.room_id=s.room_id) FROM t_seat s WHERE s.seat_id=?";
		return getForValue(sql, seatId);
	}

	@Override
	public long alreadyJoinRoom(String userId) {
		String sql = "SELECT COUNT(*) FROM t_seat s LEFT JOIN t_room r ON r.room_id=s.room_id WHERE user_id=? AND r.room_status!=0";
		return getForValue(sql, userId);
	}

}
